package com.dayatang.android.Activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.TextAppearanceSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.dayatang.android.Db.ProjectSearchServiceDb;
import com.dayatang.android.Model.SubProject;
import com.dayatang.android.config.Config;

public class SubProjectDetailsActivity extends FootCommonActivity {
	
	private ProjectSearchServiceDb projectSearchServiceDb;
	private SubProject subProject;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AlabSetContentView(R.layout.subproject_details_activity);
		projectSearchServiceDb = new ProjectSearchServiceDb(this);
		Init();
		AlabSetBarTitleText(subProject.getName());
	}
	
	private String getSubProjectIdByInteng() {
		return getIntent().getStringExtra(Config.SUBPROJECTID);
	}
	
	private void Init() {
		subProject = projectSearchServiceDb.getSubProjectBySubProjectId(getSubProjectIdByInteng());
		InitRealWorkload();
		InitFinishOutputValue();
		InitProjectBasicInfo();
		InitProgress();
		InitConstructionTeam();
	}
	
	private void InitProjectBasicInfo() {
		
		String[] info = {getString(R.string.subproject_type),getString(R.string.subproject_apType),getString(R.string.subproject_delistingEntrustType),subProject.getProjectType(),subProject.getApType(),subProject.getDelistingEntrustType(),getString(R.string.subproject_start_work_date),getString(R.string.subproject_finish_work_date),getString(R.string.subproject_basic_project_area),subProject.getStartDate(),subProject.getFinishDate(),subProject.getArea(),getString(R.string.subproject_transmissionOpenDate),getString(R.string.subproject_opticalCableFinishDate),getString(R.string.subproject_basic_responsible_division),subProject.getTransmissionOpenDate(),subProject.getOpticalCableFinishDate(),subProject.getResponsibleDivision()};
		
		boolean one = true;
		for (int i = 0; i < info.length; i = i + 3) {
			if (one) {
				InitTableLayout(info[i],info[i+1],info[i+2],"white",R.id.subproject_basic_information_tablelayout);
				one = false;
			} else {
				InitTableLayout(info[i],info[i+1],info[i+2],"black",R.id.subproject_basic_information_tablelayout);
				one = true;
			}
		}
		
		String[] aboutInfo = {getString(R.string.subproject_about_organization_ownerinfo),getString(R.string.subproject_about_organization_supervisorinfo),getString(R.string.subproject_about_organization_designerinfo),subProject.getOwnerInfo(),subProject.getSupervisorInfo(),subProject.getDesignerInfo()};
		
		for (int i = 0; i < aboutInfo.length; i = i + 3) {
			if (one) {
				InitTableLayout(aboutInfo[i],aboutInfo[i+1],aboutInfo[i+2],"white",R.id.subproject_about_organization_tablelayout);
				one = false;
			} else {
				InitTableLayout(aboutInfo[i],aboutInfo[i+1],aboutInfo[i+2],"black",R.id.subproject_about_organization_tablelayout);
				one = true;
			}
		}
	}
	
	private void InitFinishOutputValue() {
		String data = subProject.getSpecialtyOutput();
		long outputAll = 0;
		try {
			JSONArray jsonArray = new JSONArray(data);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				InitTableLayout(jsonObject.getString("specialty"),jsonObject.getString("output"),getString(R.string.subproject_finish_output_unit),"red",R.id.subproject_finish_output_value_tablelayout);
				outputAll = outputAll + Long.valueOf(jsonObject.getString("output"));
			}
			InitTableLayout(getString(R.string.subproject_output_all),String.valueOf(outputAll),getString(R.string.subproject_finish_output_unit),"red",R.id.subproject_finish_output_value_tablelayout);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void InitTableLayout(Object... params) {
		
		TableLayout table = (TableLayout) findViewById((Integer) params[4]);
		
		TextAppearanceSpan textappearancespan = null;
		
		if (params[3].equals("red")) {
			textappearancespan = new TextAppearanceSpan(this, R.style.listview_item_textview_red);   
		} else if(params[3].equals("black")) {
			textappearancespan = new TextAppearanceSpan(this, R.style.normalText);   
		} else if(params[3].equals("white")) {
			textappearancespan = new TextAppearanceSpan(this, R.style.listview_item_textview_while);   
		}
		
		TableRow row = new TableRow(this); 
		row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		
		TextView t = new TextView(this);
		t.setGravity(Gravity.CENTER_HORIZONTAL);
		SpannableStringBuilder style=new SpannableStringBuilder((CharSequence) params[0]);
		style.setSpan(textappearancespan,0,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t.setText(style);
		
		row.addView(t);

		TextView t2 = new TextView(this);
		t2.setGravity(Gravity.CENTER_HORIZONTAL);
		SpannableStringBuilder style1=new SpannableStringBuilder((CharSequence) params[1]);
		style1.setSpan(textappearancespan,0,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t2.setText(style1);
		
		row.addView(t2);
		
		TextView t3 = new TextView(this);
		t3.setGravity(Gravity.CENTER_HORIZONTAL);
		SpannableStringBuilder style2=new SpannableStringBuilder((CharSequence) params[2]);
		style2.setSpan(textappearancespan,0,0,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t3.setText(style2);
		
		row.addView(t3);
		
		table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   

	}
	
	private void InitRealWorkload() {
		String data = subProject.getWorkItemQuantity();
		try {
			JSONArray jsonArray = new JSONArray(data);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				InitTableLayout(jsonObject.getString("workItemName"),jsonObject.getString("quantity"),jsonObject.getString("workItemUnit"),"red",R.id.subproject_real_workload_tablelayout);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void InitProgress() {
		String data = subProject.getProgressInfo();
		try {
			JSONArray jsonArray = new JSONArray(data);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				InitTableLayout(jsonObject.getString("workStepName"),jsonObject.getString("responsible"),jsonObject.getString("progressStatus"),"black",R.id.subproject_progress_tablelayout);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void InitConstructionTeam() {
		String data = subProject.getConstructionTeam();
		try {
			JSONArray jsonArray = new JSONArray(data);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				InitTableLayout(jsonObject.getString("specialty"),jsonObject.getString("team"),jsonObject.getString("teamLeader"),"black",R.id.subproject_team_tablelayout);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			finishActivityPushUpIn();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
